<?php defined('BASEPATH') OR exit('No direct script access allowed');


/**
 * 后台角色管理
 */
class Menu extends Admin_Controller {


    public function __construct()
    {
        parent::__construct();

    } 


    //列表页
    public function index()
    {
        $this->load->model('admin/menu_model', 'search_menu');

        //状态数组
        $data["status_arr"] = menu_model::$status_arr;        
        //分页
        $data["pageCurrent"] = ForceIntFrom('pageCurrent');
        $data["pageSize"] = ForceIntFrom('pageSize');

        $data["pageCurrent"] = Iif($data["pageCurrent"], $data["pageCurrent"], 1);
        $data["pageSize"] = Iif($data["pageSize"], $data["pageSize"], 20);

        //排序
        $data["orderField"] = trim($this->input->post('orderField', true));
        $data["orderDirection"] = trim($this->input->post('orderDirection', true));

        $data["orderField"] = Iif($data["orderField"], $data["orderField"], 'id');
        $data["orderDirection"] = Iif($data["orderDirection"], $data["orderDirection"], 'asc');


        //搜索关键字等
        $data['keytype'] = trim($this->input->post('keytype', true));
        $data['keyword'] = trim($this->input->post('keyword', true));


        if(!empty($data['keyword']) && !empty($data['keytype']))
        {
            $this->search_menu->$data['keytype'] = $data['keyword'];
        }


        //获取记录列表
        $this->search_menu->_order_by = Iif($data['orderField'] != 'admin_counts', "adro.") . "{$data[orderField]} {$data[orderDirection]}";
        $this->search_menu->_page_index = $data["pageCurrent"];
        $this->search_menu->_page_count = $data["pageSize"];
        $this->search_menu->search();

        $list = $this->search_menu->_search_results;

        $all = $this->search_menu->get_all_menu();
        
        foreach ($list as $key => $value) {
            if($value->parent_id != 0 ){

                $list[$key]->parent = $this->get_menu_Up($list, $value->parent_id);
            }
        }

        $data["list"] = $list;

        //获取总记录数
        $this->search_menu->_is_search_counter = true;
        $this->search_menu->search();

        $data["totalCount"] = $this->search_menu->_search_num_rows;


        $this->display('menu/index.html', $data);
    }


    private function get_menu_Up($a, $parent_id){
        $re = "";
        foreach ($a as $value) {
            $k = "";
            if($value->id == $parent_id){
                $re = $value->name;
                if($value->parent_id != 0 ){
                    $k = $this->get_menu_Up($a, $value->parent_id);
                    $re = $k."-".$re;
                }
            }
        }
        return $re;
    }


    //编辑角色
    public function edit($id = 0)
    {
        $this->add($id);
    }


    //添加或编辑角色
    public function add($id = 0)
    {

        $data['menu'] = array('id' => '', 'status' => '', 'name' => '', 'url' => '', 'parent_id' => '', 'context' => '');
        // $data['permissions'] = array();

        $id = intval($id); //角色id

        //读取所有的权限资源
        $this->load->model('admin/menu_model', 'search_menu');
        $this->search_menu->_order_by = ' id ASC';
        $this->search_menu->search();

        $data['admin_menu'] = $this->search_menu->_search_results;

        $allmenu = $this->search_menu->get_menu_select();

        $data['menuSelect'] = $this->__menu_select($allmenu);
        // print_r($data['menuSelect']);exit;
        // print_r($data['menuSelect']);exit;
  
        if($id )
        {
            // $this->load->model('admin/menu_model', 'search_menu2');
           
            if(!$this->search_menu->retrieve($id))
            {
                $this->error('模块不存在！');
            }
            $this->search_menu->id = $id;
            $this->search_menu->search();
            $a = $this->search_menu->_search_results;
            //赋值
            $data['menu'] = array(
                'id' => $a[0]->id,
                'status' => $a[0]->status,
                'name' => $a[0]->name,
                'url' => $a[0]->url,
                'parent_id' => $a[0]->parent_id,
                'context' => $a[0]->context,
                'tabid' => $a[0]->tabid,
                'icon' => $a[0]->icon,
            );


            // $data['permissions'] = unserialize($this->search_menu->permissions); //角色权限数组
        }


        $this->display('menu/edit.html', $data);
    }



    private function __menu_select($list, $str ="|-"){
        $re = [];

        foreach ($list as $key => $value) {
            $kkk = [];
            $kkk["name"] = $str.$value["name"];
            $kkk["id"] = $value["id"];
            $re[] = $kkk;
            if($value["child"]){
                $kkk2= $this->__menu_select($value["child"], $str."--");
                $re = array_merge($re,$kkk2);
            }
        }
        return $re;
    }    



    //保存角色
    public function save()
    {
        //初始化权限数组
        // $permissions = array();

        $id = intval($_POST['id']); //角色id
        $status = intval($_POST['status']); //状态
        $parent_id = intval($_POST['parent_id']); //状态
        $name = trim($this->input->post('name', true)); //角色名称
        $url = trim($this->input->post('url', true)); //角色名称
        $context = trim($this->input->post('context', true)); //角色名称
        $tabid = trim($this->input->post('tabid', true)); //角色名称
        $icon = trim($this->input->post('icon', true)); //角色名称
        // $parent_id = trim($this->input->post('parent_id', true)); //角色名称
        // $parent_id = $this->input->post('parent_id', true); //数组

        if(empty($name))
        {
            $this->error('未填写菜单名称！');
        }

        if(empty($url) )
        {
            $this->error('未填写菜单地址！');
        }



        $this->load->model('admin/menu_model');

        $this->menu_model->name = $name;
        $this->menu_model->status = $status;
        $this->menu_model->parent_id = $parent_id;
        $this->menu_model->url = $url;
        $this->menu_model->context = $context;
        $this->menu_model->tabid = $tabid;
        $this->menu_model->icon = $icon;

        if($id) { //更新数据

          
            //$this->role_model->status = $status;
            $this->menu_model->_update_where = array('id' => $id);

            if($this->menu_model->update())
            {
                $this->success('菜单 -> 更新成功！', 'admin-menu-list'); //编辑时不更新tab
            }

        }else{ //插入记录
            

            if($this->menu_model->insert())
            {
                $this->success('菜单 -> 添加成功！', 'admin-menu-list'); //admin-role-list指需要更新的tabId
            }
        }

        $this->error();
    }


    //删除角色
    // public function delete($id = 0)
    // {
    //     $id = intval($id); //角色id

    //     //超级管理不允许删除
    //     if(empty($id) OR $id == '1')
    //     {
    //         $this->error('未选择需要删除的角色！');
    //     }



    //     //删除        
    //     $this->load->model('admin/menu_model');

    //     $this->menu_model->_delete_where = array('id' => $id);

    //     if($this->menu_model->delete())
    //     {
    //         $this->success('菜单 -> 删除成功！', 'admin-menu-list', false); //false表示: 不关闭当前tab或dialog
    //     }

    //     $this->error();
    // }

}
